package com.github.dice.service.impl;

import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.dice.entity.PlayerAndDiceMapping;
import com.github.dice.mapper.PlayerAndDiceMappingMapper;
import com.github.dice.service.IPlayerAndDiceMappingService;

@Service
public class PlayerAndDiceMappingService extends ServiceImpl<PlayerAndDiceMappingMapper, PlayerAndDiceMapping>
        implements IPlayerAndDiceMappingService {

    @Resource
    private PlayerAndDiceMappingMapper playerAndDiceMappingMapper;

    @Override
    public List<PlayerAndDiceMapping> getPlayerAndDiceMappingListByRoom(String roomNumber) {
        QueryWrapper<PlayerAndDiceMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_number", roomNumber);
        queryWrapper.eq("is_delete",0);
        return playerAndDiceMappingMapper.selectList(queryWrapper);
    }

    @Override
    public PlayerAndDiceMapping getPlayerAndDiceMappingListByRoomAndPlayer(String roomNumber, String playerName) {
        QueryWrapper<PlayerAndDiceMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_number", roomNumber);
        queryWrapper.eq("player_name", playerName);
        queryWrapper.eq("is_delete",0);
        return playerAndDiceMappingMapper.selectOne(queryWrapper);
    }

    @Override
    public void addPlayerAndDiceMapping(PlayerAndDiceMapping playerAndDiceMapping) {
        playerAndDiceMappingMapper.insert(playerAndDiceMapping);
    }

    @Override
    public void updatePlayerDice(PlayerAndDiceMapping playerAndDiceMapping){
        UpdateWrapper<PlayerAndDiceMapping> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("player_and_dice_mapping_id",playerAndDiceMapping.getPlayerAndDiceMappingId());
        PlayerAndDiceMapping updateData = new PlayerAndDiceMapping();
        updateData.setDiceNumber(playerAndDiceMapping.getDiceNumber());
        playerAndDiceMappingMapper.update(updateData,updateWrapper);
    }

    @Override
    public void delPlayerAndDiceMappingByRoom(@Nonnull String roomNumber) {
        UpdateWrapper<PlayerAndDiceMapping> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("room_number",roomNumber);
        PlayerAndDiceMapping playerAndDiceMapping = new PlayerAndDiceMapping();
        playerAndDiceMapping.setIsDelete(0);
        playerAndDiceMappingMapper.update(playerAndDiceMapping,updateWrapper);
    }
}
